<?php
namespace app\common\model\food;
/**
 * 交易记录模型
 */
class Record extends BaseModel
{
    // 定义表名
    protected $name = 'food_record';
    // 定义主键
    protected $pk = 'record_id';
    // 追加字段
    protected $append = [];
    
    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\food\\User','user_id');
    }
    
    /**
     * 关联门店表
     */
    public function shop()
    {
        return $this->belongsTo('app\\common\\model\\food\\Shop','shop_id');
    }
    
    /**
     * 交易模式
     */
    public function getModeAttr($value)
    {
        $status = [10 => '充值', 20 => '消费', 30 => '退款', 40 => '赠送', 50 => '扣减'];
        return ['text' => $status[$value], 'value' => $value];
    }
    /**
     * 支付类型
     */
    public function getTypeAttr($value)
    {
        $status = [10 => '余额', 20 => '现金', 30 => '微信', 40 => '支付宝', 90 => '积分'];
        return ['text' => $status[$value], 'value' => $value];
    }
    
    /**
     * 获取列表
     */
    public function getList($filter = [],$shop_id=0)
    {
        $shop_id > 0 && $filter['shop_id'] = $shop_id;
        // 执行查询
        return $this->with(['user','shop'])
            ->where($filter)
            ->order('record_id','desc')
            ->paginate(['list_rows'=>15,'query' => request()->param()]);
    }
    
    /**
     * 根据时间段统计数量
     */
    public static function getDateCount(array $data)
    {
         // 筛选条件
        $filter = [];
        $data['shop_id'] > 0 && $filter['shop_id'] = $data['shop_id'];
        $count = [
            10 => self::where('create_time','>',$data['star'])->where('create_time','<',$data['end'])->where($filter)->where('mode',10)->sum('money'),//充值
            20 => self::where('create_time','>',$data['star'])->where('create_time','<',$data['end'])->where($filter)->where('mode',20)->sum('money'),//消费
            30 => self::where('create_time','>',$data['star'])->where('create_time','<',$data['end'])->where($filter)->where('mode',30)->sum('money'),//退款
            40 => self::where('create_time','>',$data['star'])->where('create_time','<',$data['end'])->where($filter)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where('create_time','>',$data['star'])->where('create_time','<',$data['end'])->where($filter)->where('mode',50)->sum('money'),//扣减
        ];
        return $count;
    }
    
    /**
     * 根据条件统计数量
     */
    public static function getCount($shop_id = 0)
    {
        // 筛选条件
        $filter = [];
        $shop_id > 0 && $filter['shop_id'] = $shop_id;
        $count = array();
        
        //全部
        $count['all'] = [
            10 => self::where($filter)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('mode',50)->sum('money'),//扣减
        ];
        //今天
        $star = strtotime(date("Y-m-d"),time());
        $count['today'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('mode',50)->sum('money'),//扣减
        ];
        //昨天
        $star = strtotime("-1 day");
        $end = strtotime(date("Y-m-d"),time());
        $count['today2'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //前天
        $star = strtotime("-2 day");
        $end = strtotime("-1 day");
        $count['today3'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //-4天
        $star = strtotime("-3 day");
        $end = strtotime("-2 day");
        $count['today4'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //-5天
        $star = strtotime("-4 day");
        $end = strtotime("-3 day");
        $count['today5'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //-6天
        $star = strtotime("-5 day");
        $end = strtotime("-4 day");
        $count['today6'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //-7天
        $star = strtotime("-6 day");
        $end = strtotime("-5 day");
        $count['today7'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        
        //本月起至时间 - 月度统计 
        $end = mktime(0,0,0,date('m'),1,date('y'));
        $count['month'] = [
            10 => self::where($filter)->where('create_time','>',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$end)->where('mode',50)->sum('money'),//扣减
        ];
        //上月开始  
        $star = mktime(0,0,0,date('m')-1,1,date('y'));
        $count['month2'] = [
            10 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',10)->sum('money'),//充值
            20 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',20)->sum('money'),//消费
            30 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',30)->sum('money'),//退款
            40 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',40)->where('type',20)->sum('money'),//赠送
            50 => self::where($filter)->where('create_time','>',$star)->where('create_time','<',$end)->where('mode',50)->sum('money'),//扣减
        ];
        return $count;
    }
}